package com.zilla.android.lib.ui.photo;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import com.zilla.android.lib.ui.MyPopupWindow;
import com.zilla.android.lib.ui.R;

/**
 * Created with IntelliJ IDEA.
 * User: chenze
 * Date: 13-11-19
 * Time: 下午9:25
 * To change this template use File | Settings | File Templates.
 */
public class PhotoMenu implements View.OnClickListener {

    /**
     * 意图类型
     */
    public static final int INTENT_PHOTO = 0x100;
    public static final int INTENT_CAMERA = 0x101;

    public static final int CROP_BIG_PICTURE = 0x110;
    public static final int CHOOSE_BIG_PICTURE = 0x111;


    private Context context;
    private LayoutInflater inflater;

    private IPhotoCallBack iPhotoCallBack;
    private Button photoButton;
    private Button cameraButton;
    private Button cancelButton;

    private MyPopupWindow popupWindow;

    public PhotoMenu(Context context) {
        this.context = context;
        inflater = LayoutInflater.from(this.context);
        initView(context);
    }

    private void initView(Context context) {
        View contentView = inflater.inflate(R.layout.popup_photo_layout, null);
        photoButton = (Button) contentView.findViewById(R.id.popup_menu_photo);
        cameraButton = (Button) contentView.findViewById(R.id.popup_menu_camera);
        cancelButton = (Button) contentView.findViewById(R.id.popup_menu_cancle);
        cameraButton.setOnClickListener(this);
        photoButton.setOnClickListener(this);
        cancelButton.setOnClickListener(this);
        popupWindow = new MyPopupWindow(contentView);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.popup_menu_photo) {
            if (this.iPhotoCallBack != null) {
                iPhotoCallBack.onPhotoClicked();
            }
            popupWindow.dismiss();
        } else if (id == R.id.popup_menu_camera) {
            if (this.iPhotoCallBack != null) {
                iPhotoCallBack.onCameraClicked();
            }
            popupWindow.dismiss();
        } else if (id == R.id.popup_menu_cancle) {
            popupWindow.dismiss();
        }
    }

    /**
     * 设置点击监听
     *
     * @param iPhotoCallBack
     */
    public void setPhotoCallBack(IPhotoCallBack iPhotoCallBack) {
        this.iPhotoCallBack = iPhotoCallBack;
    }

    public interface IPhotoCallBack {
        void onPhotoClicked();

        void onCameraClicked();
    }

    public void switchMenu(View anchor) {
        if (popupWindow.isShowing()) {
            popupWindow.dismiss();
        } else {
            popupWindow.showAsDropDown(anchor);
        }
    }

    public void switchBottom(View anchor) {
        if (popupWindow.isShowing()) {
            popupWindow.dismiss();
        } else {
            popupWindow.setWidth(ViewGroup.LayoutParams.FILL_PARENT);
            popupWindow.showAtLocation(anchor, Gravity.BOTTOM, 0, 0);
        }
    }
}
